package cn.edu.jxau.core.service;

import cn.edu.jxau.core.controller.ServiceResultConfig;
import cn.edu.jxau.core.exception.ServiceException;
import cn.edu.jxau.framework.dao.core.Session;
import cn.edu.jxau.framework.dao.core.sqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DetectedService extends ServiceSupport {
	private static Logger logger = LoggerFactory.getLogger(DetectedService.class);

	public DetectedService() {
		super();
	}

	public DetectedService(String view, Map<String, Object[]> params, Map<String, Object> data)
			throws ServiceException {
		super(view, params, data);
		execute(view, params, data);
	}

	/**
	 * 添加被检测物
	 */

	private void addDetected(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的
		String detectedName = getParamStringValue(params, "detectedName", 0);
		String classifyID = getParamStringValue(params, "classifyID", 0);
		logger.debug(detectedName);
		logger.debug(classifyID);

		// 数据库操作

		// 插入数据
		param.put("detectedName", detectedName);
		param.put("classifyID", classifyID);
		
		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("addDetected", param, datas);
			System.out.println("return： " + datas + "\n" + param);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				data.put("result", ServiceResultConfig.SUCCESS);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 添加检测内容
	 */
	private void addContent(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String categoryID = getParamStringValue(params, "categoryID", 0);
		String name = getParamStringValue(params, "name", 0);

		logger.debug(categoryID);
		logger.debug(name);

		// 数据库操作

		// 插入数据
		param.put("categoryID", categoryID);
		param.put("name", name);
		
		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("addContent", param, datas);
			System.out.println("return： " + datas + "\n" + param);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				data.put("result", ServiceResultConfig.SUCCESS);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 添加分类
	 */
	private void addCategory(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String type = getParamStringValue(params, "type", 0);
		String name = getParamStringValue(params, "name", 0);
		logger.debug(type + "======" + name);
		// 数据库操作

		// 插入数据
		// param.put("name", name);
		
		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			if (("0").equals(type)) {
				param.put("firstClassification", name);
				session.execute("addCategoryDetected", param, datas);
			} else if (("1").equals(type)) {
				param.put("secondaryClassification", name);
				session.execute("addCategoryDetected", param, datas);
			} else if (("2").equals(type)) {
				param.put("name", name);
				session.execute("addCategoryContent", param, datas);
			}

			System.out.println("return： " + datas + "\n" + param);

			if (null == name) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				data.put("result", ServiceResultConfig.SUCCESS);
			}

		
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 修改被检测物
	 */

	private void updateDetected(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String detectedID = getParamStringValue(params, "detectedID", 0);
		String detectedName = getParamStringValue(params, "detectedName", 0);
		String classifyID = getParamStringValue(params, "classifyID", 0);

		logger.debug(detectedID);
		logger.debug(classifyID);
		// 数据库操作

		// 插入数据
		param.put("detectedID", detectedID);
		param.put("detectedName", detectedName);
		param.put("classifyID", classifyID);

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("updateDetected", param, datas);
			System.out.println("return： " + param);

			if (null == detectedID) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				data.put("result", ServiceResultConfig.SUCCESS);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 修改检测内容
	 */

	private void updateContent(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String contentID = getParamStringValue(params, "contentID", 0);
		String categoryID = getParamStringValue(params, "categoryID", 0);
		String name = getParamStringValue(params, "name", 0);

		// 数据库操作

		// 插入数据
		param.put("contentID", contentID);
		param.put("categoryID", categoryID);
		param.put("name", name);

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("updateContent", param, datas);
			//System.out.println("return： " + param);

			if (null == contentID) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				data.put("result", ServiceResultConfig.SUCCESS);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 修改分类
	 */
	private void updateCategory(Map<String, Object[]> params, Map<String, Object> data) {

	}

	/**
	 * 删除被检测物和检测内容
	 */
	private void delete(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String id = getParamStringValue(params, "id", 0);
		String type = getParamStringValue(params, "type", 0);
		// 数据库操作

		// 删除操作

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			// 0 被检测物 1 检测内容 2 被检测物分类 3 检测内容分类
			if ("0".equals(type)) {
				param.put("detectedID", id);
				session.execute("deleteDetected", param, datas);
			} else if ("1".equals(type)) {
				param.put("contentID", id);
				session.execute("deleteContent", param, datas);
			} 

			if (null == id || null == type) {
				data.put("result", FAILED);
			} else {
				data.put("result", SUCCESS);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 查询所有被检测物
	 */
	private void listAllDetect(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("listAllDetect", param, datas);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
				return;
			} else {
				// 保存session的数据
				data.put("session_sub", datas);
				data.put("result", ServiceResultConfig.SUCCESS);
				/*for (Map.Entry<String, Object> entry : data.entrySet()) {
					System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

				}*/
				return;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 查询所有被检测物的分类
	 */
	private void listAllDetectCategory(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("listAllDetectCategory", param, datas);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				// 保存session的数据
				data.put("data", datas);
				data.put("result", ServiceResultConfig.SUCCESS);
				for (Map.Entry<String, Object> entry : data.entrySet()) {
					System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 查询所有检测内容的分类
	 */
	private void listAllContentCategory(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		
		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("listAllContentCategory", param, datas);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				// 保存session的数据
				data.put("session_cc", datas);
				data.put("result", ServiceResultConfig.SUCCESS);
				/*for (Map.Entry<String, Object> entry : data.entrySet()) {
					System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

				}*/
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 查询所有检测内容
	 */
	private void listAllContent(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("listAllContent", param, datas);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				// 保存session的数据
				data.put("data", datas);
				//logger.debug(JSON.toJSONString(data, SerializerFeature.PrettyFormat));
				data.put("result", ServiceResultConfig.SUCCESS);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();
		}
	}

	/**
	 * 查询单条被检测物
	 */
	private void listAllDetectById(Map<String, Object[]> params, Map<String, Object> data) {
		Map<String, Object> param = new HashMap<String, Object>(); // 用来封装传递给数据库的参数
		List<HashMap<String, Object>> datas = new ArrayList(); // 用来接收数据库返回回来的

		String detectedID = getParamStringValue(params, "detectedID", 0);
		param.put("detectedID", detectedID);
		
		Session session=null;
		try {
			session = sqlSessionFactory.getInstance().getSession();
			session.execute("listAllDetectById", param, datas);

			if (null == datas || datas.isEmpty()) {
				data.put("result", ServiceResultConfig.FAILED);
			} else {
				// 保存session的数据
				data.put("data", datas.get(0));
				data.put("result", ServiceResultConfig.SUCCESS);
			}
					} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			// 释放会话
			session.releaseConnection();

		}
	}
}
